package material

import (
	"strconv"

	"oxy_server/until"

	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"

	"oxy_server/model"
	"oxy_server/module/log"
)

type (
	Typecode struct {
		ID   bson.ObjectId `json:"_id" bson:"_id"`
		Name string        `json:"name" bson:"name"`
		Code string        `json:"code" bson:"code"`
	}
	insertTypecode struct {
		Name string `json:"name" bson:"name"`
		Code string `json:"code" bson:"code"`
	}
)

type OXYMaterialCode struct{}

func NewOxyMaterial() *OXYMaterialCode {
	oxygender := &OXYMaterialCode{}
	return oxygender
}

func (g *OXYMaterialCode) GetListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "material_dic"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().QuerySort(M, currentpage, perpage, "_id")
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []Typecode{}
	query.All(&ps)
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  ps,
		"count": count,
	})

	return nil
}

func (g *OXYMaterialCode) AddHandler(c echo.Context) error {
	Typecode := &insertTypecode{}
	if err := c.Bind(Typecode); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "material_dic"
	err := model.DB().Insert(Typecode)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (g *OXYMaterialCode) UpdateHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateGenderHandler", id)

	Typecode := &insertTypecode{}
	if err := c.Bind(Typecode); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "material_dic"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, Typecode)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (g *OXYMaterialCode) DelHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelGenderHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "material_dic"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (ro *OXYMaterialCode) AddsHandler(c echo.Context) error {
	s := []insertTypecode{}
	if err := c.Bind(&s); err != nil {
		log.Error("AddPropertysHandler 解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	var str = ""
	for _, item := range s {
		model.DB().Document = "material_dic"
		err := model.DB().Insert(item)
		if err != nil {
			str += "," + item.Name
			log.Error("插入出错 AddPropertysHandler", err)
		}
	}
	if len(str) == 0 {
		c.JSON(200, map[string]interface{}{
			"msg": "success",
		})
	} else {
		c.JSON(403, map[string]interface{}{
			"msg":  "error",
			"info": str,
		})
	}

	return nil
}
